package cn.net.ecode.elec.api.web;

import cn.net.ecode.common.utils.IdGen;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.elec.bulk.entity.ElecBulkManage;
import cn.net.ecode.elec.bulk.entity.ElecConvenientBulk;
import cn.net.ecode.elec.bulk.service.ElecBulkManageService;
import cn.net.ecode.elec.bulk.service.ElecConvenientBulkService;
import cn.net.ecode.elec.stickers.entity.ElecConvenientStickers;
import cn.net.ecode.elec.stickers.service.ElecConvenientStickersService;
import cn.net.ecode.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName AppAdController
 * @Description 广告相关接口
 * @Author AlanShelby
 * @Date 2019-08-26 11:23:46 11:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/convenient")
public class AppConvenientController {

    @Autowired
    private ElecConvenientBulkService bulkService;

    @Autowired
    private ElecConvenientStickersService stickersService;

    @Autowired
    private ElecBulkManageService bulkManageService;

    /**
     * @Method getBulkInfo
     * @Description 获取团购信息
     * @Author AlanShelby
     * @Date 9:00 2019/9/27 0027
     * @Return cn.net.ecode.util.R
     */
    @RequestMapping(value = "/getBulkInfo", method = RequestMethod.GET)
    public R getBulkInfo() {
        ElecBulkManage bulkManage = new ElecBulkManage();
        bulkManage.setBulkStatus("2");
        List<ElecBulkManage> bulkList = bulkManageService.findList(bulkManage);
        if (bulkList.isEmpty()) {
            return R.error("暂无团购");
        }
        return R.ok().put("bulkList", bulkList);
    }


    /**
     * @Method saveBulk
     * @Description 提报便利团
     * @Author AlanShelby
     * @Date 10:26 2019/9/26 0026
     * @Param [elecEarningsCalculate]
     * @Return cn.net.ecode.util.R
     */
    @RequestMapping(value = "/saveBulk", method = RequestMethod.POST)
    public R saveBulk(@RequestBody ElecConvenientBulk elecConvenientBulk) {
        if (StringUtils.isBlank(elecConvenientBulk.getBtypeId()) || StringUtils.isBlank(elecConvenientBulk.getBtypeName())) {
            return R.error("企业信息不能为空");
        }

        if (elecConvenientBulk.getElectricity() == null) {
            return R.error("电量不能为空");
        }

        elecConvenientBulk.setIsNewRecord(true);
        elecConvenientBulk.setId(IdGen.nextId());
        bulkService.save(elecConvenientBulk);

        return R.ok();
    }

    /**
     * @Method saveStickers
     * @Description 便利贴提报
     * @Author AlanShelby
     * @Date 10:38 2019/9/26 0026
     * @Param [elecConvenientStickers]
     * @Return cn.net.ecode.util.R
     */
    @RequestMapping(value = "/saveStickers", method = RequestMethod.POST)
    public R saveStickers(@RequestBody ElecConvenientStickers elecConvenientStickers) {
        if (StringUtils.isBlank(elecConvenientStickers.getBtypeId()) || StringUtils.isBlank(elecConvenientStickers.getBtypeName())) {
            return R.error("企业信息不能为空");
        }

        if (elecConvenientStickers.getElectricity() == null || elecConvenientStickers.getPrice() == null || StringUtils.isBlank(elecConvenientStickers.getYear())) {
            return R.error("上报信息不能为空");
        }

        elecConvenientStickers.setIsNewRecord(true);
        elecConvenientStickers.setId(IdGen.nextId());
        stickersService.save(elecConvenientStickers);

        return R.ok();
    }
}
